{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chi Square Test of Independence"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "from scipy.stats import chi2\n",
    "from scipy.stats import chi2_contingency"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<matplotlib.lines.Line2D at 0x2031607f610>]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYAAAAD4CAYAAADlwTGnAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8vihELAAAACXBIWXMAAAsTAAALEwEAmpwYAAAhbElEQVR4nO3dfZBd913f8fdH+yDtriytnizLKzkSRk0swFY8QjGEJqF2qO1S5DATKs+QmEw6imesSUzJtAKmnXSYKZngEBpqrDog6lCICRATkRE4Rgm00CRo7TiOZVt44yetJEurR+txd++93/5xzpWu11fac3fvuWcfPq+Zm3uez+93FZ/v/h6PIgIzM5t95hSdADMzK4YDgJnZLOUAYGY2SzkAmJnNUg4AZmazVHvRCWjE0qVLY/Xq1UUnw8xsWnnyySePRsSysdunVQBYvXo1/f39RSfDzGxakfRqve2uAjIzm6UcAMzMZikHADOzWcoBwMxslnIAMDObpRwAzMxmKQcAM7NZygGghb772gmeGTxZdDLMzAAHgJb6L1/dy2987bmik2FmBkyzkcDT3f4T5+jp9E9uZlODn0Ytcna4xMlzo5y+UKJcCdrmqOgkmdks5yqgFjlw8jwA5Upw9MxwwakxM3MAaJkDJ85fXD548vwVjjQzaw0HgBYZrHnov37qQoEpMTNLZAoAkm6XtE/SgKRtdfa/Q9K3JA1L+mTN9rd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8ETB3OucN+BJJe8YoyV/4H71SfoBfT58x+4A7JnpfTwVhZjZLuQrIzGyWcgAwM5ulHADMzGYpBwAzs1nKAcDMbJZyADAzm6UcAMzMZqn/D09Q7OHKlUn4AAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "x_min = -10\n",
    "x_max = 100\n",
    "\n",
    "x = np.linspace(x_min, x_max, 100)\n",
    "y = chi2.pdf(x, df=4)\n",
    "plt.plot(x, y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Loading the Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = pd.read_csv('data/adult.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>age</th>\n",
       "      <th>workclass</th>\n",
       "      <th>fnlwgt</th>\n",
       "      <th>education</th>\n",
       "      <th>educational-num</th>\n",
       "      <th>marital-status</th>\n",
       "      <th>occupation</th>\n",
       "      <th>relationship</th>\n",
       "      <th>race</th>\n",
       "      <th>gender</th>\n",
       "      <th>capital-gain</th>\n",
       "      <th>capital-loss</th>\n",
       "      <th>hours-per-week</th>\n",
       "      <th>native-country</th>\n",
       "      <th>income</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>25</td>\n",
       "      <td>Private</td>\n",
       "      <td>226802</td>\n",
       "      <td>11th</td>\n",
       "      <td>7</td>\n",
       "      <td>Never-married</td>\n",
       "      <td>Machine-op-inspct</td>\n",
       "      <td>Own-child</td>\n",
       "      <td>Black</td>\n",
       "      <td>Male</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>40</td>\n",
       "      <td>United-States</td>\n",
       "      <td>&lt;=50K</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>38</td>\n",
       "      <td>Private</td>\n",
       "      <td>89814</td>\n",
       "      <td>HS-grad</td>\n",
       "      <td>9</td>\n",
       "      <td>Married-civ-spouse</td>\n",
       "      <td>Farming-fishing</td>\n",
       "      <td>Husband</td>\n",
       "      <td>White</td>\n",
       "      <td>Male</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>50</td>\n",
       "      <td>United-States</td>\n",
       "      <td>&lt;=50K</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>28</td>\n",
       "      <td>Local-gov</td>\n",
       "      <td>336951</td>\n",
       "      <td>Assoc-acdm</td>\n",
       "      <td>12</td>\n",
       "      <td>Married-civ-spouse</td>\n",
       "      <td>Protective-serv</td>\n",
       "      <td>Husband</td>\n",
       "      <td>White</td>\n",
       "      <td>Male</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>40</td>\n",
       "      <td>United-States</td>\n",
       "      <td>&gt;50K</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>44</td>\n",
       "      <td>Private</td>\n",
       "      <td>160323</td>\n",
       "      <td>Some-college</td>\n",
       "      <td>10</td>\n",
       "      <td>Married-civ-spouse</td>\n",
       "      <td>Machine-op-inspct</td>\n",
       "      <td>Husband</td>\n",
       "      <td>Black</td>\n",
       "      <td>Male</td>\n",
       "      <td>7688</td>\n",
       "      <td>0</td>\n",
       "      <td>40</td>\n",
       "      <td>United-States</td>\n",
       "      <td>&gt;50K</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>18</td>\n",
       "      <td>?</td>\n",
       "      <td>103497</td>\n",
       "      <td>Some-college</td>\n",
       "      <td>10</td>\n",
       "      <td>Never-married</td>\n",
       "      <td>?</td>\n",
       "      <td>Own-child</td>\n",
       "      <td>White</td>\n",
       "      <td>Female</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>30</td>\n",
       "      <td>United-States</td>\n",
       "      <td>&lt;=50K</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   age  workclass  fnlwgt     education  educational-num      marital-status  \\\n",
       "0   25    Private  226802          11th                7       Never-married   \n",
       "1   38    Private   89814       HS-grad                9  Married-civ-spouse   \n",
       "2   28  Local-gov  336951    Assoc-acdm               12  Married-civ-spouse   \n",
       "3   44    Private  160323  Some-college               10  Married-civ-spouse   \n",
       "4   18          ?  103497  Some-college               10       Never-married   \n",
       "\n",
       "          occupation relationship   race  gender  capital-gain  capital-loss  \\\n",
       "0  Machine-op-inspct    Own-child  Black    Male             0             0   \n",
       "1    Farming-fishing      Husband  White    Male             0             0   \n",
       "2    Protective-serv      Husband  White    Male             0             0   \n",
       "3  Machine-op-inspct      Husband  Black    Male          7688             0   \n",
       "4                  ?    Own-child  White  Female             0             0   \n",
       "\n",
       "   hours-per-week native-country income  \n",
       "0              40  United-States  <=50K  \n",
       "1              50  United-States  <=50K  \n",
       "2              40  United-States   >50K  \n",
       "3              40  United-States   >50K  \n",
       "4              30  United-States  <=50K  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['age', 'workclass', 'fnlwgt', 'education', 'educational-num',\n",
       "       'marital-status', 'occupation', 'relationship', 'race', 'gender',\n",
       "       'capital-gain', 'capital-loss', 'hours-per-week', 'native-country',\n",
       "       'income'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.columns"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Ques- Is there a relationship between occupation and gender? (or does the preference of occupation depend on the gender)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Male      32650\n",
       "Female    16192\n",
       "Name: gender, dtype: int64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.gender.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Prof-specialty       6172\n",
       "Craft-repair         6112\n",
       "Exec-managerial      6086\n",
       "Adm-clerical         5611\n",
       "Sales                5504\n",
       "Other-service        4923\n",
       "Machine-op-inspct    3022\n",
       "?                    2809\n",
       "Transport-moving     2355\n",
       "Handlers-cleaners    2072\n",
       "Farming-fishing      1490\n",
       "Tech-support         1446\n",
       "Protective-serv       983\n",
       "Priv-house-serv       242\n",
       "Armed-Forces           15\n",
       "Name: occupation, dtype: int64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.occupation.value_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Understanding the Chi2 Test\n",
    "\n",
    "Lets make a bold Claim that **Gender** and **Occupation** are dependent.\n",
    "\n",
    "Step - 1:  \n",
    "i.e. Alternate Hypothesis: $$ H_1: They \\ are \\ Dependent $$\n",
    "and Null Hypothesis: $$ H_0: They \\ are \\ Independent $$\n",
    "\n",
    "Step - 2:  \n",
    "* Collect the sample of size n\n",
    "* Compute the sample frequencies\n",
    "\n",
    "Step - 3: Compute $ \\chi^2 $ test statistic  \n",
    "Now you need to check, if the difference in the observed and expected frequencies is too extreme to reject the NULL hypothesis.\n",
    "* Have a look at Observed Frequencies (in the sample)\n",
    "* Compute the Expected Frequencies (under null hyp assumption) $$ Expected \\ Value = \\frac{row \\ total * col \\ total}{grand \\ total}$$\n",
    "Now, test statistic can be computed using below mentioned formula: $$ \\chi^2 = \\Sigma \\frac{(o-e)^2}{e} $$\n",
    "\n",
    "Step - 4: Decide $ \\alpha $ and $ df = (rows-1)(cols-1) $\n",
    "\n",
    "Step - 5: Apply decision rule  \n",
    "* Chi Square Test $$ if \\ \\chi^2 > \\chi^2_{df, \\alpha} \\Rightarrow Reject H_0 $$\n",
    "* p-value Test $$ p \\ value = (1.0 - cdf(test \\ statistic)) $$\n",
    "Now, $$ if (p \\ value < \\alpha) \\Rightarrow Accept H_1 \\ or \\ Reject H_0 $$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>gender</th>\n",
       "      <th>Female</th>\n",
       "      <th>Male</th>\n",
       "      <th>All</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>occupation</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>?</th>\n",
       "      <td>1273</td>\n",
       "      <td>1536</td>\n",
       "      <td>2809</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Adm-clerical</th>\n",
       "      <td>3769</td>\n",
       "      <td>1842</td>\n",
       "      <td>5611</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Armed-Forces</th>\n",
       "      <td>0</td>\n",
       "      <td>15</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Craft-repair</th>\n",
       "      <td>323</td>\n",
       "      <td>5789</td>\n",
       "      <td>6112</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Exec-managerial</th>\n",
       "      <td>1748</td>\n",
       "      <td>4338</td>\n",
       "      <td>6086</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Farming-fishing</th>\n",
       "      <td>95</td>\n",
       "      <td>1395</td>\n",
       "      <td>1490</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Handlers-cleaners</th>\n",
       "      <td>254</td>\n",
       "      <td>1818</td>\n",
       "      <td>2072</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Machine-op-inspct</th>\n",
       "      <td>804</td>\n",
       "      <td>2218</td>\n",
       "      <td>3022</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Other-service</th>\n",
       "      <td>2698</td>\n",
       "      <td>2225</td>\n",
       "      <td>4923</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Priv-house-serv</th>\n",
       "      <td>228</td>\n",
       "      <td>14</td>\n",
       "      <td>242</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Prof-specialty</th>\n",
       "      <td>2242</td>\n",
       "      <td>3930</td>\n",
       "      <td>6172</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Protective-serv</th>\n",
       "      <td>122</td>\n",
       "      <td>861</td>\n",
       "      <td>983</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Sales</th>\n",
       "      <td>1947</td>\n",
       "      <td>3557</td>\n",
       "      <td>5504</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Tech-support</th>\n",
       "      <td>562</td>\n",
       "      <td>884</td>\n",
       "      <td>1446</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Transport-moving</th>\n",
       "      <td>127</td>\n",
       "      <td>2228</td>\n",
       "      <td>2355</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>All</th>\n",
       "      <td>16192</td>\n",
       "      <td>32650</td>\n",
       "      <td>48842</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "gender             Female   Male    All\n",
       "occupation                             \n",
       "?                    1273   1536   2809\n",
       "Adm-clerical         3769   1842   5611\n",
       "Armed-Forces            0     15     15\n",
       "Craft-repair          323   5789   6112\n",
       "Exec-managerial      1748   4338   6086\n",
       "Farming-fishing        95   1395   1490\n",
       "Handlers-cleaners     254   1818   2072\n",
       "Machine-op-inspct     804   2218   3022\n",
       "Other-service        2698   2225   4923\n",
       "Priv-house-serv       228     14    242\n",
       "Prof-specialty       2242   3930   6172\n",
       "Protective-serv       122    861    983\n",
       "Sales                1947   3557   5504\n",
       "Tech-support          562    884   1446\n",
       "Transport-moving      127   2228   2355\n",
       "All                 16192  32650  48842"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Step - 2 => Looking at the freqency distribution\n",
    "\n",
    "pd.crosstab(data.occupation, data.gender, margins=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>gender</th>\n",
       "      <th>Female</th>\n",
       "      <th>Male</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>occupation</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>?</th>\n",
       "      <td>1273</td>\n",
       "      <td>1536</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Adm-clerical</th>\n",
       "      <td>3769</td>\n",
       "      <td>1842</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Armed-Forces</th>\n",
       "      <td>0</td>\n",
       "      <td>15</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Craft-repair</th>\n",
       "      <td>323</td>\n",
       "      <td>5789</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Exec-managerial</th>\n",
       "      <td>1748</td>\n",
       "      <td>4338</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Farming-fishing</th>\n",
       "      <td>95</td>\n",
       "      <td>1395</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Handlers-cleaners</th>\n",
       "      <td>254</td>\n",
       "      <td>1818</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Machine-op-inspct</th>\n",
       "      <td>804</td>\n",
       "      <td>2218</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Other-service</th>\n",
       "      <td>2698</td>\n",
       "      <td>2225</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Priv-house-serv</th>\n",
       "      <td>228</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Prof-specialty</th>\n",
       "      <td>2242</td>\n",
       "      <td>3930</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Protective-serv</th>\n",
       "      <td>122</td>\n",
       "      <td>861</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Sales</th>\n",
       "      <td>1947</td>\n",
       "      <td>3557</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Tech-support</th>\n",
       "      <td>562</td>\n",
       "      <td>884</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Transport-moving</th>\n",
       "      <td>127</td>\n",
       "      <td>2228</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "gender             Female  Male\n",
       "occupation                     \n",
       "?                    1273  1536\n",
       "Adm-clerical         3769  1842\n",
       "Armed-Forces            0    15\n",
       "Craft-repair          323  5789\n",
       "Exec-managerial      1748  4338\n",
       "Farming-fishing        95  1395\n",
       "Handlers-cleaners     254  1818\n",
       "Machine-op-inspct     804  2218\n",
       "Other-service        2698  2225\n",
       "Priv-house-serv       228    14\n",
       "Prof-specialty       2242  3930\n",
       "Protective-serv       122   861\n",
       "Sales                1947  3557\n",
       "Tech-support          562   884\n",
       "Transport-moving      127  2228"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# These are the observed frequencies\n",
    "\n",
    "observed = pd.crosstab(data.occupation, data.gender)\n",
    "\n",
    "observed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(8789.50801388705,\n",
       " 0.0,\n",
       " 14,\n",
       " array([[ 931.233938  , 1877.766062  ],\n",
       "        [1860.14725032, 3750.85274968],\n",
       "        [   4.97276934,   10.02723066],\n",
       "        [2026.2377462 , 4085.7622538 ],\n",
       "        [2017.61827935, 4068.38172065],\n",
       "        [ 493.96175423,  996.03824577],\n",
       "        [ 686.90520454, 1385.09479546],\n",
       "        [1001.8472626 , 2020.1527374 ],\n",
       "        [1632.06289669, 3290.93710331],\n",
       "        [  80.22734532,  161.77265468],\n",
       "        [2046.12882355, 4125.87117645],\n",
       "        [ 325.88215061,  657.11784939],\n",
       "        [1824.67482904, 3679.32517096],\n",
       "        [ 479.37496417,  966.62503583],\n",
       "        [ 780.72478604, 1574.27521396]]))"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# chi2_contigency returns chi2 test statistic, p-value, degree of freedoms, expected frequencies\n",
    "\n",
    "chi2_contingency(observed)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Computing chi2 test statistic, p-value, degree of freedoms\n",
    "\n",
    "chi2_test_stat = chi2_contingency(observed)[0]\n",
    "pval = chi2_contingency(observed)[1]\n",
    "df = chi2_contingency(observed)[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "22.307129581578693"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "confidence_level = 0.90\n",
    "\n",
    "alpha = 1 - confidence_level\n",
    "\n",
    "chi2_critical = chi2.ppf(1 - alpha, df)\n",
    "\n",
    "chi2_critical"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.collections.PolyCollection at 0x203182a5e20>"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYQAAAD6CAYAAACh4jDWAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8vihELAAAACXBIWXMAAAsTAAALEwEAmpwYAAAnIElEQVR4nO3de3Cc9X3v8fdXq6t1tyVbNxsbYnw9YBzFONemIRebpnHndKaFTkqbtuP6AE16meaQ0zlnmplz5rTTNG04IbiE0JQmgSQkaV3iYmgCBcLNsjHG8i2yDFiWLMvYut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loq5ktCv693ES0ry0Tj8WE6T77HuAWM8szs1XAauClGV/N3VPmB7gZOAmcAv4i7Hrm+bN/gOgp3WHgUPBzM7CE6N0DPw/+XBx2rfN8XD4MPBr8npHHAtgENAR/N/4FKM/gY/FF4DhwBPhnIC9TjgXwENG+kxGiZwC/f7nPDvxF8F16Atgez3topLKIiACpdclIRERCpEAQERFAgSAiIgEFgoiIAAoEEREJKBBERARQIIiISECBICIiAPx/Juchu+OO+TgAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Ploting the chi2 distribution to visualise\n",
    "\n",
    "# Defining the x minimum and x maximum\n",
    "x_min = 0\n",
    "x_max = 100\n",
    "\n",
    "# Ploting the graph and setting the x limits\n",
    "x = np.linspace(x_min, x_max, 100)\n",
    "y = chi2.pdf(x, df)\n",
    "plt.xlim(x_min, x_max)\n",
    "plt.plot(x, y)\n",
    "\n",
    "\n",
    "# Setting Chi2 Critical value \n",
    "chi2_critical_right = chi2_critical\n",
    "\n",
    "# Shading the right rejection region\n",
    "x1 = np.linspace(chi2_critical_right, x_max, 100)\n",
    "y1 = chi2.pdf(x1, df)\n",
    "plt.fill_between(x1, y1, color='orange')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Reject Null Hypothesis\n"
     ]
    }
   ],
   "source": [
    "if(chi2_test_stat > chi2_critical):\n",
    "    print(\"Reject Null Hypothesis\")\n",
    "else:\n",
    "    print(\"Fail to Reject Null Hypothesis\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Reject Null Hypothesis\n"
     ]
    }
   ],
   "source": [
    "if(pval < alpha):\n",
    "    print(\"Reject Null Hypothesis\")\n",
    "else:\n",
    "    print(\"Fail to Reject Null Hypothesis\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
